package lgkj.meicqt20.ui;

import android.media.MediaPlayer;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import lgkj.meicqt20.R;
import lgkj.meicqt20.bean.MessageEvent;
import lgkj.meicqt20.utils.AllKindsOfConstant;
import lgkj.meicqt20.utils.CommUtils;
import lgkj.meicqt20.utils.HttpUtils;
import lgkj.meicqt20.view.CustomVideoView;

/**
 * Created by Administrator on 2019/1/18 0018.
 */

public class MainDetailsActicity extends BaseActivity implements MediaPlayer.OnErrorListener {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.jin_xi)
    ImageView jinXi;
    @BindView(R.id.iv_code)
    ImageView ivCode;
    @BindView(R.id.rl_settime)
    RelativeLayout rlSettime;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.vw_vieo)
    CustomVideoView vwVieo;
    @BindView(R.id.iv_img)
    ImageView ivImg;
    @BindView(R.id.tv_tips)
    TextView tvTips;
    private MediaController mediaController;
    private List<String> list = new ArrayList<>();
    private String id = "1";

    @Override
    public int setContentView() {
        return R.layout.activity_main_details;
    }

    @Override
    public void init() {
        displayDifferen();
        HttpUtils.getQRCode(MainDetailsActicity.this, ivCode);
        mediaController = new MediaController(MainDetailsActicity.this);
        vwVieo.setMediaController(mediaController);
    }


    @Override
    public void onResume() {
        super.onResume();
        activitiesList.add(this);
        EventBus.getDefault().register(this);
        Log.d(TAG, "onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
        EventBus.getDefault().unregister(this);
        try {
            if (vwVieo.isPlaying()) {
                vwVieo.pause();
            }
            vwVieo.stopPlayback();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(MessageEvent messageEvent) {
        Log.i(TAG, "socket: " + messageEvent.toString());
        if ("control".equals(messageEvent.getTagName()))
            fragmentDealOrder(messageEvent.getMessage());
    }

    private void fragmentDealOrder(String order) {
        switch (order) {
            case "up":
                break;
            case "down":
                break;
            case "sure":
                break;
            case "back":
                finish();
                break;
            case "left":
                postion--;
                play();
                break;
            case "right":
                postion++;
                play();
                break;
        }
    }

    public void initData() {
        list.clear();
        id = getIntent().getStringExtra("video_id");
        Map<String, String> haspMap = new HashMap<>();
        if (!"".equals(id)) {
            haspMap.put("id", id + "");
        }
        HttpUtils.getHttp(haspMap, AllKindsOfConstant.homeDetails, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                try {
                    if (jsonObject.optInt("status") == 1) {
                        JSONObject data = jsonObject.getJSONArray("data").getJSONObject(0);
                        tvTitle.setText(data.getString("ad_name"));
                        tvName.setText(data.getString("ad_text"));
                        JSONArray adinfo = data.getJSONArray("adinfo");

                        for (int i = 0; i < adinfo.length(); i++) {
                            list.add(adinfo.getJSONObject(i).getString("adinfo_url"));
                        }
                        if (list.size() > 0) {
                            play();
                        } else {
                            tvTips.setText("0/0");
                        }
                    }
                } catch (JSONException e) {
                    Log.e(TAG, "JSONException: " + e.getMessage());
                    tvTips.setText("0/0");
                }
            }

            @Override
            public void errorHttp(String msg) {
                tvTitle.setText("0/0");
            }
        });
    }

    private int postion = 0;

    private void play() {
        try {
            if (postion < list.size() && list.size() > 0) {
                Log.d(TAG, "play: " + list.toString());
                Uri url = Uri.parse(list.get(postion));
                Log.i(TAG, "play: " + postion + ",name:" + list.get(postion) + "===" + CommUtils.isMp4(list.get(postion)));
                tvTips.setText((postion + 1) + "/" + list.size());
                if (CommUtils.isMp4(list.get(postion))) {
                    vwVieo.setOnErrorListener(this);
                    vwVieo.play(url);
                } else {
                    vwVieo.setVisibility(View.GONE);
                    ivImg.setVisibility(View.VISIBLE);
                    CommUtils.LoadImage(MainDetailsActicity.this, ivImg, list.get(postion), 0);
                }
            }
        } catch (Exception e) {
            Log.d(TAG, "play: " + e.getMessage());
        }
    }

    @Override
    public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
        return true;
    }

}
